package io.jpress.addon.interact.controller;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import io.jboot.db.model.Columns;
import io.jboot.web.controller.annotation.RequestMapping;
import io.jboot.web.validate.EmptyValidate;
import io.jboot.web.validate.Form;
import io.jpress.addon.interact.model.JpressAddonInteract;
import io.jpress.addon.interact.model.JpressAddonInteractComment;
import io.jpress.addon.interact.service.JpressAddonInteractCommentService;
import io.jpress.addon.interact.service.JpressAddonInteractService;
import io.jpress.core.menu.annotation.AdminMenu;
import io.jpress.model.User;
import io.jpress.web.base.AdminControllerBase;

import java.util.Date;
import java.util.List;


@RequestMapping(value = "/admin/interact/jpress_addon_interact", viewPath = "/")
public class _JpressAddonInteractController extends AdminControllerBase {

    @Inject
    private JpressAddonInteractService service;
    @Inject
    private JpressAddonInteractCommentService commentService;

    @AdminMenu(text = "互动信息", groupId = "interact")
    public void index() {

        String nickname = getPara("nickname");
        String title = getPara("title");
        Date startTime = getParaToDate("startTime");
        Date endTime = getParaToDate("endTime");

        Record first = Db.findFirst("select * from user_admin where nickname = ?", nickname);

        Columns columns = Columns.create();
        if(first!=null){
            columns.eq("user_id",first.get("id"));
        }else{
            columns.eq("user_id",nickname!=null?0:null);
        }

        columns.likeAppendPercent("title",title);
        columns.ge("create_time",startTime).le("create_time",endTime);

        Page<JpressAddonInteract> entries=service.paginateByColumns(getPagePara(), 10,columns,"create_time desc");
        setAttr("page", entries);
        render("views/jpress_addon_interact_list.html");
    }


    public void edit() {
        int entryId = getParaToInt(0, 0);
        JpressAddonInteract entry = entryId > 0 ? service.findById(entryId) : null;
        setAttr("jpressAddonInteract", entry);
        render("views/jpress_addon_interact_edit.html");
    }

    //跳转到解答界面
    public void answer(){
        int entryId = getParaToInt(0, 0);
        JpressAddonInteract entry = entryId > 0 ? service.findById(entryId) : null;
        setAttr("jpressAddonInteract", entry);
        render("views/jpress_addon_interact_answer.html");
    }

    //保存解答内容
    public void doSaveAnswer(){
        JpressAddonInteractComment entry = getModel(JpressAddonInteractComment.class,"jpressAddonInteractComment");
        User user = getLoginedUser();
        if (user != null) {
            entry.setUserId(user.getId().intValue());
        }else{
            renderJson(Ret.fail());
            return;
        }
        entry.setCreateTime(new Date());
        commentService.save(entry);
        JpressAddonInteract byId = service.findById(entry.getInteractId());
        byId.setCreateTime(new Date());
        service.update(byId);

        renderJson(Ret.ok().set("id", entry.getId()));
    }

    //跳转到解答列表
    public void comment(){

        Columns columns = new Columns();
        columns.eq("interact_id",getPara("interactid"));
        Page<JpressAddonInteractComment> entries = commentService.paginateByColumns(getPagePara(), 10, columns);
        setAttr("page", entries);
        render("views/jpress_addon_interact_comment_list.html");

    }

    public void doSave() {
        JpressAddonInteract entry = getModel(JpressAddonInteract.class,"jpressAddonInteract");

        User user = getLoginedUser();
        if (user != null) {
            entry.setUserId(user.getId().intValue());
        }else{
            renderJson(Ret.fail());
            return;
        }
        entry.setCreateTime(new Date());
        service.saveOrUpdate(entry);

        renderJson(Ret.ok().set("id", entry.getId()));
    }

    @Before(Tx.class)
    public void doDel() {
        Long id = getIdPara();
        Columns columns = new Columns();
        columns.eq("interact_id",id);
        List<JpressAddonInteractComment> listByColumns = commentService.findListByColumns(columns);
        for(JpressAddonInteractComment jpressAddonInteractComment:listByColumns){
            commentService.deleteById(jpressAddonInteractComment.getId());
        }
        service.deleteById(id);
        render(Ret.ok());
    }

    @Before(Tx.class)
    @EmptyValidate(@Form(name = "ids"))
    public void doDelByIds() {

        Object[] ids = getParaSet("ids").toArray();

        for(Object object : ids){
            Columns columns = new Columns();
            columns.eq("interact_id",object);
            List<JpressAddonInteractComment> listByColumns = commentService.findListByColumns(columns);
            for(JpressAddonInteractComment jpressAddonInteractComment:listByColumns){
                commentService.deleteById(jpressAddonInteractComment.getId());
            }
        }

        service.batchDeleteByIds(ids);
        renderOkJson();
    }
}
